local sanquan = fk.CreateSkill {
  name = "sanquan",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
  ["sanquan"] = "三权",
  ["$sanquan1"] = "CHINA!",
  ["@@sanquan_hp"] = "<font color='red'>总统权</font>",
  ["@@sanquan_card"] = "<font color='blue'>国会权</font>",
  ["#sanquan_card-discard"] = "国会权:你的手牌数不能超过4张",
  ["@@sanquan_skill"] = "<font color='white'>最高法院权</font>",
  [":sanquan"] = "锁定技，<br>当一名其他角色的体力值首次＝3时，你对其使用<font color='red'>总统权</font>（你有10％×其当前体力上限的概率令其将阵营转化为与你相同，然后若其原身份为主公，你更改身份为主公）" ..
      "<br>当一名其他角色的手牌数首次＝4时，你对其使用<font color='blue'>国会权</font>（你令其手牌数至多为4）" ..
      "<br>当一名其他角色的技能数首次＝3时，你对其使用<font color='white'>最高法院权</font>（其本局技能数大于3时，其失去之）",
}

local M = require "packages.internetMEME.record.INTTE"

sanquan:addEffect(fk.HpChanged, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(sanquan.name) and target ~= player and target.hp == 3 and
        target:getMark("sanquan_hpfail") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "sanquan_hpfail", 1)
    if math.random(1, 100) <= 10 * target.maxHp then
      room:setPlayerMark(target, "@@sanquan_hp", 1)
      local role = target.role
      player.room:setPlayerProperty(target, "role", player.role)
      if role == "lord" then
        player.room:setPlayerProperty(player, "role", "lord")
      end
    end
  end
})

sanquan:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player:usedSkillTimes(sanquan.name) > 30 then return false end
    if player:hasSkill(sanquan.name) then
      for _, move in ipairs(data) do
        if move.to and move.to ~= player and move.toArea == Card.PlayerHand then
          if (move.to:getHandcardNum() == 4 and move.to:getMark("@@sanquan_card") == 0) or (move.to:getHandcardNum() > 4 and move.to:getMark("@@sanquan_card") == 1) then
            return true
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if move.from and move.from ~= player and info.fromArea == Card.PlayerHand then
            if move.from:getHandcardNum() == 4 and move.from:getMark("@@sanquan_card") == 0 then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.to and move.to ~= player and move.toArea == Card.PlayerHand then
        if move.to:getHandcardNum() == 4 then
          player.room:setPlayerMark(move.to, "@@sanquan_card", 1)
        end
        if move.to:getHandcardNum() > 4 and move.to:getMark("@@sanquan_card") == 1 then
          M.ChangeHandNum(move.to, 4,
            "#sanquan_card-discard", self)
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if move.from and move.from ~= player and info.fromArea == Card.PlayerHand then
          if move.from:getHandcardNum() == 4 then
            player.room:setPlayerMark(move.from, "@@sanquan_card", 1)
          end
        end
      end
    end
  end
})

sanquan:addEffect(fk.EventAcquireSkill, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sanquan.name) and target ~= player then
      if target:getMark("@@sanquan_skill") == 0 then
        return #target:getSkillNameList() == 3
      else
        return #target:getSkillNameList() > 3
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if target:getMark("@@sanquan_skill") == 0 then
      player.room:setPlayerMark(target, "@@sanquan_skill", 1)
    elseif target:getMark("@@sanquan_skill") == 1 then
      player.room:handleAddLoseSkills(target, "-" .. data.skill.name, nil)
    end
  end
})

sanquan:addEffect(fk.EventLoseSkill, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sanquan.name) and target ~= player then
      return #target:getSkillNameList() == 3
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@@sanquan_skill", 1)
  end
})

sanquan:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(sanquan.name, false, true)
  end,
  on_refresh = function(self, event, target, player, data)
    for _, p in ipairs(player.room:getAlivePlayers()) do
      player.room:setPlayerMark(p, "@@sanquan_skill", 0)
      player.room:setPlayerMark(p, "@@sanquan_card", 0)
      player.room:setPlayerMark(p, "@@sanquan_hp", 0)
    end
  end
})

return sanquan
